/*
 * @Author: yangni 920556271@qq.com
 * @Date: 2024-03-07 08:55:59
 * @LastEditors: yangni 920556271@qq.com
 * @LastEditTime: 2024-05-11 17:49:29
 * @FilePath: /yt-devops/lib/pages/add_charge_end/add_charge_end_view.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:reactive_forms/reactive_forms.dart';
import 'package:yt_devops/components/brn/brn_bottom_button_panel.dart';
import 'package:yt_devops/components/yt_button.dart';
import 'package:yt_devops/components/yt_form/widgets/form_text_field.dart';
import 'package:yt_devops/components/yt_form/widgets/reactive_brn_switch.dart';
import 'package:yt_devops/components/yt_form/widgets/reactive_text_select.dart';
import 'package:yt_devops/pages/devices/device_list/device_list_model.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/utils/brn_row1_delegate.dart';
import 'add_charge_terminal_controller.dart';

class AddChargeTerminalPage extends GetView<AddChargeTerminalController> {
  AddChargeTerminalPage({Key? key}) : super(key: key);

  double titleWidth = 100.w;

  TextStyle labelStyle =
      TextStyles.styleWith(14.sp, color: Colour.otherTextColor);
  TextStyle valueStyle(bool? enable) => TextStyles.styleWith(14.sp,
      color: enable == false ? Colour.lightTextColor : Colour.mainTextColor);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(controller.title)),
      body: GetBuilder<AddChargeTerminalController>(
        builder: (controller) => ReactiveFormBuilder(
            onWillPop: () async {
              if (FocusScope.of(context).hasFocus) {
                FocusScope.of(context).unfocus;
              }
              return true;
            },
            form: controller.buildForm,
            builder: (context, form, child) {
              controller.form = form;

              return Column(children: [
                Container(
                    color: Colour.white,
                    padding: EdgeInsets.only(left: 10.w, right: 16.w),
                    child: Column(children: [
                      ReactiveTextSelect<ProductDetailModel,
                              ProductDetailModel>(
                          formControlName: 'product',
                          titleWidth: titleWidth,
                          title: ResKey.deviceModel.tr,
                          maxLines: 2,
                          minLines: 1,
                          style: valueStyle(
                              controller.form.controls['product']?.enabled),
                          labelStyle: labelStyle,
                          value: (ProductDetailModel? v) => v?.name ?? '',
                          onTap: (control) {
                            controller.toSelectProduct(context);
                          }),
                      FormTextField(
                        formControlName: 'name',
                        titleWidth: titleWidth,
                        labelStyle: labelStyle,
                        maxLines: 2,
                        minLines: 1,
                        enable: false,
                        title: ResKey.deviceConfigName.tr,
                      ),
                      FormTextField(
                        formControlName: 'sn',
                        titleWidth: titleWidth,
                        labelStyle: labelStyle,
                        maxLines: 2,
                        minLines: 1,
                        style:
                            valueStyle(controller.form.controls['sn']?.enabled),
                        title: ResKey.deviceSerialNumber.tr,
                      ),
                    ])),
                Spacer(),
                ReactiveFormConsumer(builder: (context, form, child) {
                  return Container(
                    color: Colour.white,
                    padding: const EdgeInsets.only(bottom: 34),
                    child: YtBrnBottomButtonPanel(
                      enableMainButton: form.valid,
                      mainButtonName: ResKey.save.tr,
                      secondaryButtonName: ResKey.cancel.tr,
                      secondaryButtonBorder:
                          Border.all(color: Colour.dividerColor),
                      mainButtonOnTap: () {
                        controller.save(form);
                      },
                      secondaryButtonOnTap: () => Get.back(),
                    ),
                  );
                }),
              ]);
              ;
            }),
      ),
    );
  }
}
